<?php
// +----------------------------------------------------------------------
// | 随访系统
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2050 http://www.hedesoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: 小李子 < 490959289@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
use think\Validate;
use think\Cache;
class Login extends Controller
{
    public function index()
    {
        $this->assign('sys_config',$this->getConfig());
    	return view();
    }


    public function loginOut()
    {
        session(null);
        cookie(null);
        echo json_encode(['status'=>1,'msg'=> '成功登出']);exit();
    }
    /**
     * 用户后台登陆
     * @return array
     */
    public function login()
    {

        $post=$this->request->param();
        $rule = [
            'account'  => 'require|max:255',
            'password'  => 'require|min:6|max:255',
        ];

        $msg = [
            'account.require' => '管理员账号不能为空',
            'account.max' => '管理员账号不能超过255个字符',
            'password.require' => '账号密码不能为空',
            'password.min' => '账号密码不能少于6个字符',
            'password.max' => '账号密码不能超过255个字符',
        ];

        $validate = new Validate($rule, $msg);
        if (!$validate->check($post)){
            echo json_encode(['status'=>0,'msg'=>$validate->getError()]);exit();
        }

        $map['admin_name'] = $post['account'];
        $user = model('AdminUser')->where($map)->find();
        if (empty($user)) {
            echo json_encode(['status'=>0,'msg'=> '账号不存在']);exit();
        }
        if ($user['admin_password'] != md5($post['password'])) {
            echo json_encode(['status'=>0,'msg'=> '登录密码不正确']);exit();
        }
        //标记session
        session('user', $user);
        session('admin_login', 1);
        //登录时间修改
        model('AdminUser')->where($map)->setField('login_time',time());
        echo json_encode(['status'=>1,'msg'=> '登录成功','url'=>url('Index/index')]);exit();

    }
    /**
     * 获取配置信息
     * @access public
     */
    public function getConfig()
    {
        $config=Cache::get('config');
        if(empty($config)){
            $configs = Db::name('Config')->select();
            foreach ($configs as $key=>$value){
                $config[$value['key_name']]=$value['key_value'];
            }
            Cache::set('config',$config);
        }
        return $config;
    }



}
